package com.example.testapplication.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.testapplication.R;
import com.example.testapplication.adapter.MainHeaderAdAdapter;
import com.example.testapplication.adapter.MainMenuAdapter;
import com.example.testapplication.adapter.SecondMenuAdapter;
import com.example.testapplication.util.DataUtil;

// 主界面
public class MainFragment extends Fragment {
    // banner
    protected int [] icons = {R.mipmap.header_pic_ad1,R.mipmap.header_pic_ad2,R.mipmap.header_pic_ad1};
    protected ViewPager mVPagerHeaderAd; //

    // 菜单图标
    protected int [] menuIons = {R.mipmap.menu_airport,R.mipmap.menu_car,R.mipmap.menu_course,R.mipmap.menu_hatol,
            R.mipmap.menu_nearby,R.mipmap.menu_ticket,
            R.mipmap.menu_ticket,R.mipmap.menu_train};
    // 滚动菜单
    protected  int[] menuSecond = {R.mipmap.menu_second_airport,R.mipmap.menu_second_play,R.mipmap.menu_second_quan,
            R.mipmap.menu_second_service,
            R.mipmap.menu_second_wifi,R.mipmap.menu_second_ticket};
    protected String[] menus;
    protected String[] scondMenus;
    protected RecyclerView mRecyclerViewMenu; // 菜单
    protected RecyclerView sRecyclerViewMenu; // 水平滚动菜单
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main,container,false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        menus = this.getActivity().getResources().getStringArray(R.array.main_menu);
        scondMenus = this.getActivity().getResources().getStringArray(R.array.second_menu);

        // banner
        mVPagerHeaderAd = (ViewPager) getView().findViewById(R.id.vpager_main_header_ad);
        MainHeaderAdAdapter adapter = new MainHeaderAdAdapter(getActivity(), DataUtil.getHeaderAddInfo(getActivity(),icons));
        mVPagerHeaderAd.setAdapter(adapter);

        // 菜单
        mRecyclerViewMenu = (RecyclerView) getView().findViewById(R.id.recycleview_main_menu);
        mRecyclerViewMenu.setLayoutManager(new GridLayoutManager(getActivity(),4));
        MainMenuAdapter mainMenuAdapter = new MainMenuAdapter(getActivity(),DataUtil.getMainMenus(menuIons,menus));
        mRecyclerViewMenu.setAdapter(mainMenuAdapter);

        // 水平滚动菜单
        sRecyclerViewMenu = (RecyclerView) getView().findViewById(R.id.recycleview_second_menu);
        //创建布局管理器, 传入 上下文实例 , 方向 , 是否翻转 参数
        //横向设置 , 翻转
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(),RecyclerView.HORIZONTAL,true);
        sRecyclerViewMenu.setLayoutManager(linearLayoutManager);
        SecondMenuAdapter secondMenuAdapter = new SecondMenuAdapter(getActivity(),DataUtil.getMainMenus(menuSecond,scondMenus));
        sRecyclerViewMenu.setAdapter(secondMenuAdapter);







    }
}
